package com.itheima.tanhua.controller;

import com.itheima.common.ResultInfo;
import com.itheima.domain.db.Admin;
import com.itheima.tanhua.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author zsk
 * @create 2022-03-30-12:34
 */
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private HttpSession session;


    //接受登录请求
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Map<String, String> map){

        //接收参数
        String username = map.get("username");
        String password = map.get("password");

        //调用service
        ResultInfo resultInfo = adminService.login(username,password);

        if (resultInfo.getCode() == 1){
            //代表登录成功
            Admin admin = (Admin) resultInfo.getData();

            //向session中保存用户数据
            session.setAttribute("SESSION_ADMIN",admin);
        }
        //返回
        return resultInfo;

    }

    //用户退出
    @PostMapping("/employee/logout")
    public ResultInfo logout() {
        //1. 注销session
        session.invalidate();

        //2. 返回成功标识
        return ResultInfo.success(null);
    }



}
